package view;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;

import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

/**
 * Left navigation area for the application
 * @author MUSAB
 *
 */
public class NavigationView extends JPanel{
	
	// Buttons
	private JButton composeBtn, createNewFolderBtn;
	
	// List of folders
	private DefaultListModel folders;
	
	// JList to show list of folders
	private JList foldersList;
	
	// New folder text field
	private JTextField newFolderName;
	
	// Parent folder selector drop down list
	private JComboBox parentFolderSelector;
	
	/**
	 * Constructor
	 * @param migLayout
	 */
	public NavigationView(MigLayout migLayout) {
		
		super(migLayout);
		
		// Add the compose button
		composeBtn = new JButton("Compose");
		
		this.add(composeBtn, "wrap, width :200:");
		
		// Setup the folders list
		folders = new DefaultListModel<String>();
		folders.addElement("Inbox");
		folders.addElement("Sent");
		folders.addElement("Drafts");
		folders.addElement("Outbox");
		foldersList = new JList(folders);
		this.add(foldersList, "grow, wrap");
		
		// Setup add new folder area
		JPanel newFolderPanel = new JPanel (new MigLayout("fillx"));
		newFolderPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.LIGHT_GRAY));
		
		JLabel createNewFolderLabel = new JLabel("Create new folder: ");
		newFolderPanel.add(createNewFolderLabel, "wrap");
		
	//	JTextField newFolderName = new JTextField(); // <-- this is an error, duplicate declaration
		newFolderName = new JTextField();  // this is correct, private member initialization.
		newFolderPanel.add(newFolderName, "growx, wrap");
		
		JLabel underLabel = new JLabel("under the parent folder: ");
		newFolderPanel.add(underLabel, "wrap");
		
		//JComboBox parentFolderSelector = new JComboBox(); // same error as above
		parentFolderSelector = new JComboBox();         // the correct way.
		parentFolderSelector.addItem("<Root>");
		parentFolderSelector.addItem("Some folder");
		parentFolderSelector.addItem("Subscriptions");
		parentFolderSelector.addItem("Work stuff");
		newFolderPanel.add(parentFolderSelector, "growx, wrap");
		
		JButton createNewFolderBtn = new JButton("Create");
		newFolderPanel.add(createNewFolderBtn, "width 40, gapbefore push");
		
		ButtonHandler handler = new ButtonHandler();
		
		createNewFolderBtn.addActionListener(handler);
		
		this.add(newFolderPanel, "grow");

	}

	private class ButtonHandler implements ActionListener
	{
		public void actionPerformed(ActionEvent event)
		{		  		
		    String newFName = newFolderName.getText();
		  
		    if(!folders.contains(newFName)&& !newFName.isEmpty())
		    {
		    	folders.addElement(newFName);
		    }
		    
		}
	}
	/**
	 * @return the composeBtn
	 */
	public JButton getComposeBtn() {
		return composeBtn;
	}

	/**
	 * @return the folders
	 */
	public DefaultListModel getFolders() {
		return folders;
	}

	/**
	 * @return the foldersList
	 */
	public JList getFoldersList() {
		return foldersList;
	}

	/**
	 * @return the newFolderName
	 */
	public JTextField getNewFolderName() {
		return newFolderName;
	}

	/**
	 * @return the createNewFolderBtn
	 */
	public JButton getCreateNewFolderBtn() {
		return createNewFolderBtn;
	}

	/**
	 * @return the parentFolderSelector
	 */
	public JComboBox getParentFolderSelector() {
		return parentFolderSelector;
	}

	/**
	 * Creates a new compose email window
	 */
	public void createNewComposeWindow() {
		new ComposeMailView();
	}

}
